//package com.notary.aspect;
//
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.extra.servlet.ServletUtil;
//import cn.hutool.http.useragent.UserAgent;
//import cn.hutool.http.useragent.UserAgentUtil;
//import cn.hutool.json.JSONObject;
//import com.notary.entity.SysLog;
//import com.notary.entity.vo.JsonResult;
//import com.notary.mapper.SysLogMapper;
//import com.notary.util.JwtUtil;
//import io.jsonwebtoken.lang.Objects;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.*;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
//import org.springframework.scheduling.annotation.Async;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//import java.lang.reflect.Method;
//import java.util.Date;
//
///**
// * 日志处理切面
// */
//@Aspect
//@Configuration
//@Slf4j
//public class SysLogAspect {
//
//    private static String[] MATCHERS = {"/banzheng/user/captcha.jpg","/banzheng/index.html"};
//
//    @Resource
//    SysLogMapper sysLogMapper;
//
//    @Pointcut("execution(* com.notary.controller.*Controller.*(..))")
//    public void pointcut() {
//    }
//
//    @Pointcut("execution(* com.notary.controller.*Controller.*(..))")
//    public void exception() {
//    }
//
//    @Async
//    @Around(value = "pointcut()")
//    public Object saveLog(ProceedingJoinPoint joinPoint) throws Throwable {
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        // 记录执行时间
//        long startTime = System.currentTimeMillis();
//        String url = request.getRequestURI();
//        if (Objects.containsElement(MATCHERS, url)) {
//            return null;
//        }
//        JsonResult result = (JsonResult) joinPoint.proceed(joinPoint.getArgs());
//        // 插入系统日志表
//        SysLog sysLog = new SysLog();
//        sysLog.setActionUrl(url);
//        sysLog.setRequestMethod(request.getMethod());
//        String uaStr = request.getHeader("User-Agent");
//        UserAgent ua = UserAgentUtil.parse(uaStr);
//        sysLog.setIp(ServletUtil.getClientIP(request));
//        sysLog.setBrowser(ua.getBrowser().toString());
//        sysLog.setOs(ua.getOs().toString());
//        long endTime = System.currentTimeMillis();
//        long totalTime = endTime - startTime;
//        log.info("**********   Url: {}, Start: {}, End: {}, Total: {}ms, Code: {}   **********", url, DateUtil.format(new Date(startTime), "yyyy-MM-dd HH:mm:ss:SSS"), DateUtil.format(new Date(endTime), "yyyy-MM-dd HH:mm:ss:SSS"), totalTime, result);
//
//        sysLog.setStartTime(new Date(startTime));
//        sysLog.setConsumingTime(totalTime);
//        sysLog.setFinishTime(new Date(endTime));
//        String token = request.getHeader(JwtUtil.TOKEN_HEADER);
//        if (token != null) {
//            token = token.replace(JwtUtil.TOKEN_PREFIX, "");
//            String userName = JwtUtil.getUsername(token);
//            sysLog.setUserName(userName);
//        }
//        // 从切面织入点处通过反射机制获取织入点处的方法
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        // 获取切入点所在的方法
//        Method method = signature.getMethod();
//        sysLog.setActionMethod(method.getName());
//        Object[] args = joinPoint.getArgs();
//        LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
//        String[] paramNames = u.getParameterNames(method);
//        if (args != null && paramNames != null) {
//            JSONObject params = new JSONObject();
//            for (int i = 0; i < args.length; i++) {
//                if (args[i].toString().length() < 65536)
//                    params.put(paramNames[i], args[i]);
//            }
//            sysLog.setParams(params.toString());
//        }
//        if (result != null && result.getResultSuccess()) {
//            sysLog.setResultCode(result.getCode());
//            sysLog.setResultMsg(result.getMsg());
//        }
//        sysLogMapper.insert(sysLog);
//        return result;
//    }
//
//
//    /**
//     * 异常返回通知，用于拦截异常日志信息 连接点抛出异常后执行
//     *
//     * @param joinPoint 切入点
//     * @param e         异常信息
//     */
//    @Async
//    @AfterThrowing(pointcut = "exception()", throwing = "e")
//    public void saveExceptionLog(JoinPoint joinPoint, Throwable e) {
//        // 获取RequestAttributes
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        // 从获取RequestAttributes中获取HttpServletRequest的信息
//        HttpServletRequest request = attributes.getRequest();
//        try {
//            // 记录执行时间
//            long startTime = System.currentTimeMillis();
//            String url = request.getRequestURI();
//            if (Objects.containsElement(MATCHERS, url)) {
//                return;
//            }
//            // 插入系统日志表
//            SysLog sysLog = new SysLog();
//            sysLog.setActionUrl(url);
//            sysLog.setRequestMethod(request.getMethod());
//            String uaStr = request.getHeader("User-Agent");
//            UserAgent ua = UserAgentUtil.parse(uaStr);
//            sysLog.setIp(ServletUtil.getClientIP(request));
//            sysLog.setBrowser(ua.getBrowser().toString());
//            sysLog.setOs(ua.getOs().toString());
//            long endTime = System.currentTimeMillis();
//            long totalTime = endTime - startTime;
//            sysLog.setStartTime(new Date(startTime));
//            sysLog.setConsumingTime(totalTime);
//            sysLog.setFinishTime(new Date(endTime));
//            String token = request.getHeader(JwtUtil.TOKEN_HEADER);
//            if (token != null) {
//                token = token.replace(JwtUtil.TOKEN_PREFIX, "");
//                String userName = JwtUtil.getUsername(token);
//                sysLog.setUserName(userName);
//            }
//            // 从切面织入点处通过反射机制获取织入点处的方法
//            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//            // 获取切入点所在的方法
//            Method method = signature.getMethod();
//            sysLog.setActionMethod(method.getName());
//            Object[] args = joinPoint.getArgs();
//            LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
//            String[] paramNames = u.getParameterNames(method);
//            if (args != null && paramNames != null) {
//                JSONObject params = new JSONObject();
//                for (int i = 0; i < args.length; i++) {
//                    if (args[i].toString().length() < 65536)
//                        params.put(paramNames[i], args[i]);
//                }
//                sysLog.setParams(params.toString());
//            }
//            sysLog.setResultCode("500");
//            sysLog.setResultMsg(e.getMessage());
//            sysLogMapper.insert(sysLog);
//        } catch (Exception e2) {
//            e2.printStackTrace();
//        }
//    }
//
//}
